<?php
/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2021/10/14
 * Time: 12:17
 */

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Meeting extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'priority_match_user_ids';
    protected $ruletable = 'meet_rule';
    protected $userNum = 40;

    public function getlist($input=[]){
        $limit = $input['limit']??10;

        $res = DB::connection($this->connection)->table($this->table)
            ->join('user',$this->table.'.user_id', '=', 'user.user_id','left')
            ->select([$this->table.'.id','user.uuid','user.nickname','user.portrait','user.gender'])
            ->paginate($limit);
        return $this->objToArray($res);
    }

    public function getUseridById($id){
        $res = DB::connection($this->connection)->table($this->table)
            ->find($id,['user_id']);
        return $this->objToArray($res);
    }

    public function addMeetingUser($input){
        return curl_request(getKkaudioApiUrl() . '/inner/meeting/add', $input, 'json', 'POST');
    }

    public function delMeetingUser($input){
        return curl_request(getKkaudioApiUrl() . '/inner/meeting/del', $input, 'json', 'POST');
    }
    public function getListRule(){
        $limit = $input['limit']??20;
        $res = DB::connection($this->connection)->table($this->ruletable)
            ->orderBy('min_score','desc')
            ->paginate($limit);
        return $this->objToArray($res);
    }
    public function getCheckStatus($min_score,$max_score,$id = 0){
        $sql = DB::connection($this->connection)->table($this->ruletable)
            ->where('min_score','<=',$min_score)
            ->where('max_score','>=',$min_score);
            if ($id) $sql->where('id','!=',$id);
           $res = $sql->first();
        if ($res){
            return false;
        }
        $sql = DB::connection($this->connection)->table($this->ruletable)
            ->where('min_score','<=',$max_score)
            ->where('max_score','>=',$max_score);
        if ($id) $sql->where('id','!=',$id);
        $res = $sql->first();
        if ($res){
            return false;
        }
        return true;
    }

    public function getCheckUserNum($usernum,$id){
        $sql = DB::connection($this->connection)->table($this->ruletable);
        if ($id) $sql->where('id','!=',$id);
        $userNumCount = $sql->sum('user_num');
        if ((intval($this->userNum) - intval($userNumCount)) < $usernum){
            return false;
        }
        return true;
    }
    public function addOrUpdateRule($input){
        return curl_request(getKkaudioApiUrl() . '/inner/meetrule/add_or_update', $input, 'json', 'POST');
    }
    public function delRule($input){
        return curl_request(getKkaudioApiUrl() . '/inner/meetrule/del', $input, 'json', 'POST');
    }
}
